using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class tbdoanhnghiepDAL
    {
         private SqlConnection sqlCn = null;
        static public string error = "";
        public tbdoanhnghiepDAL()
        {
            string stringConnection = ConfigurationSettings.AppSettings["connectionstring"].ToString();
            sqlCn = new SqlConnection(stringConnection);
            sqlCn.Open();
        }
        ~tbdoanhnghiepDAL()
        {
            //this.sqlCn.Close();
            //this.sqlCn.Dispose();
        }

        public void Insert(tbdoanhnghiepDTO doanhnghiep)
        {
            SqlCommand cm = new SqlCommand("sp_doanhnghiep_Insert", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendn", doanhnghiep.Tendn);
            cm.Parameters.AddWithValue("@diachi", doanhnghiep.Diachi);
            cm.Parameters.AddWithValue("@dienthoai", doanhnghiep.Dienthoai);
            cm.Parameters.AddWithValue("@taikhoan", doanhnghiep.Taikhoan);
            cm.Parameters.AddWithValue("@matkhau", doanhnghiep.Matkhau);
            cm.Parameters.AddWithValue("@sodu", doanhnghiep.Sodu);
            try
            {
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return;
            }
        }

        public void Update(tbdoanhnghiepDTO doanhnghiep)
        {
            SqlCommand cm = new SqlCommand("sp_doanhnghiep_Update", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", doanhnghiep.Madn);
            cm.Parameters.AddWithValue("@tendn", doanhnghiep.Tendn);
            cm.Parameters.AddWithValue("@diachi", doanhnghiep.Diachi);
            cm.Parameters.AddWithValue("@dienthoai", doanhnghiep.Dienthoai);
            cm.Parameters.AddWithValue("@taikhoan", doanhnghiep.Taikhoan);
            cm.Parameters.AddWithValue("@matkhau", doanhnghiep.Matkhau);
            cm.Parameters.AddWithValue("@sodu", doanhnghiep.Sodu);
            try
            {
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return;
            }
        }

        public void Delete(int Ma)
        {
            SqlCommand cm = new SqlCommand("sp_doanhnghiep_Delete", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@madn", Ma);

            cm.ExecuteNonQuery();
        }

        public tbdoanhnghiepDTOColection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                       string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_doanhnghiep_GET", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;


            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            tbdoanhnghiepDTOColection tbdoanhnghiepDTOColection = new tbdoanhnghiepDTOColection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                tbdoanhnghiepDTO doanhnghiep= new tbdoanhnghiepDTO();
                doanhnghiep.Madn= int.Parse(ds.Tables[0].Rows[i]["madn"].ToString());
                doanhnghiep.Tendn = ds.Tables[0].Rows[i]["tendn"].ToString();
                doanhnghiep.Diachi = ds.Tables[0].Rows[i]["diachi"].ToString();
                doanhnghiep.Dienthoai =ds.Tables[0].Rows[i]["dienthoai"].ToString();
                doanhnghiep.Taikhoan = ds.Tables[0].Rows[i]["taikhoan"].ToString();
                doanhnghiep.Matkhau = ds.Tables[0].Rows[i]["matkhau"].ToString();
                doanhnghiep.Sodu = float.Parse(ds.Tables[0].Rows[i]["sodu"].ToString());
                tbdoanhnghiepDTOColection.Add(doanhnghiep);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());

            return tbdoanhnghiepDTOColection;
        }
    }
}
